<?php

class ExecutorController extends Controller
{
   
    public $menu = array(
        array(
            'label' =>  '执行器',
        ),
        array(
            'label' =>  '执行机列表',
            'icon'  =>  'fire',
            'url'   =>  array('/executor/index'),
        ),
        array(
            'label' =>  '新增执行机',
            'icon'  =>  'plus',
            'url'   =>  array('/executor/create'),
        ),
    );
    
    /**
     * @return array action filters
     * 
     */
    public function filters()
    {
        return array(
            'accessControl', // perform access control for CRUD operations
        );
    }

    public function accessRules()
    {
        return array(

            array('allow', // allow authenticated users to access all actions
                'users'=>array('@'),
                'expression'=>'$user->role == Role::Admin',
            ),
            array('deny',  // deny all users
                'users'=>array('*'),
            ),
        );
    }
	/**
	 * This is the default 'index' action that is invoked
	 * when an action is not explicitly requested by users.
	 */
	public function actionIndex()
	{
		// renders the view file 'protected/views/schedule/index.php'
		// using the default layout 'protected/views/layouts/main.php'

        $criteria = new CDbCriteria;
        $dataProvider = new CActiveDataProvider('ExecutorMachine', array(
            'pagination'    =>  array(
                'pageSize'  =>  20,
            ),
            'criteria'  =>  $criteria,
        ));
        $this->render('index', array(
            'dataProvider'  => $dataProvider,
        ));
    }

    public function actionCreate()
    {
        $model=new ExecutorMachine;
        if(isset($_POST['ExecutorMachine']))
        {
            $model->attributes = $_POST['ExecutorMachine'];
            $model->state = $_POST['ExecutorMachine']['state'];
            if($model->save()) {
                $this->redirect(array('index'));
            }
        }

        $this->render('create',array(
            'model'=>$model,
        ));
    }
    
    /**
     *  Updates a particular model.
     *  If update is successful, the browser will be redirected to the 'view' page.
     */
    public function actionUpdate($id)
    {
        $model=ExecutorMachine::model()->findByPk($id);
        if(isset($_POST['ExecutorMachine']))
        {
            $model->attributes=$_POST['ExecutorMachine'];
            $model->state= $_POST['ExecutorMachine']['state'];
            if($model->save())
                $this->redirect(array('index'));
        }

        $this->render('create',array(
            'model'=>$model,
        ));
    }

    /**
     *   Deletes a particular model.
     *   If deletion is successful, the browser will be redirected to the 'index' page.
     */
    public function actionDelete($id)
    {
        if(Yii::app()->request->isPostRequest)
        {
            // we only allow deletion via POST request
            if(isset($id))
            {
                $model=ExecutorMachine::model()->findByPk($id);
                $model->delete();
            }

            // if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
            if(!isset($_GET['ajax']))
                $this->redirect(array('listProduct'));
        }
        else
            throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
    }

}
